package com.动态代理01.CGLIB动态代理;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class MyMethodInterceptor01 implements MethodInterceptor {

    public static Object getProxy(Object target) {
        Enhancer enhancer = new Enhancer();
        enhancer.setClassLoader(target.getClass().getClassLoader());
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(new MyMethodInterceptor01());
        return enhancer.create();
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        System.out.println("写信前准备--研磨备纸");
        Object invokeSuper = proxy.invokeSuper(obj, args);
        System.out.println("写信后动作--邮票寄出");
        return invokeSuper;
    }
}
